<template>
  <el-dialog :title="title" width="400px" height="500" :visible.sync="params.visible">
    <compTree
      ref="tree"
      :comp-tree-height="'400px'"
      :tree-show-checkbox="params.isMulti"
      :tree-data="treeData"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
      :tree-default-expanded-keys="treeDefaultExpandedKeys"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
/**
 * 入参
 *  title 当前弹窗显示的标题
 *  ids 选择的数据
 *  isMulti 是否多选
 * 出参
 *  name 用户
 *  id 用户id
 */
import { getUserList } from '@/api/user'
import { ParamArr, SQLTable } from '@/../public/js/paramarr'
import Global from '@/../public/js/global'

export default {
  props: {
    params: {
      type: Object,
      default() {
        return {
          title: '',
          ids: '',
          // 开启勾选模式 多选
          isMulti: false,
          // 是否可以空选
          isNull: false
        }
      }
    }
  },
  data() {
    return {
      visible: false,
      treeData: [],
      data: {},
      title: '选择用户',
      node: {}, // 当前点击的节点
      selectParams: {},
      treeDefaultCheckedKeys: [],
      treeDefaultExpandedKeys: []
    }
  },
  created() {
    this.title = this.params.title
    this.treeDefaultCheckedKeys = []
    this.treeDefaultExpandedKeys = []
    this.initWindow()
  },
  methods: {
    confirm() {
      var ids = []
      var names = []
      const userIds = []
      // 开启多选
      if (this.params.isMulti) {
        const nodes = this.$refs.tree.getCheckedNodes() || []
        for (let i = 0; i < nodes.length; i++) {
          if (!nodes[i].folder) {
            ids.push(nodes[i].id)
            names.push(nodes[i].name)
            userIds.push(nodes[i].userId)
          }
        }
      } else {
        // 单选
        const node = this.$refs.tree.getCurrentNode()
        if (node && !node.folder) {
          ids.push(node.id)
          names.push(node.name)
          userIds.push(node.userId)
        }
      }
      // 判断是否不允许为空
      if (ids.length === 0 && !this.params.isNull) {
        return this.$message.warning('请选择用户！')
      }
      this.params.confirm({
        id: ids.join(','),
        name: names.join(','),
        userId: userIds.join(',')
      })
    },
    initWindow() {
      getUserList(new ParamArr()).then((vPar) => {
        this.lpfInitWindow(vPar)
      })
    },
    lpfInitWindow(vPar) {
      const nodes = []
      const nodeMap = new Map()
      let table = new SQLTable(vPar.getTable('QryGroup'))
      table.first()
      while (!table.eof()) {
        const node = {}
        const pnode = Global.getTreeviewPNode(
          nodeMap,
          nodes,
          table.getValueByName('PID')
        )
        // data
        node['id'] = table.getValueByName('ID')
        node['name'] = table.getValueByName('Name')
        node['text'] = node['name']
        node['type'] = 0
        node['folder'] = true
        // add
        pnode.push(node)
        nodeMap.set(node.id, node)
        table.next()
      }
      table = new SQLTable(vPar.getTable('QryUser'))
      table.first()
      while (!table.eof()) {
        const node = {}
        const pnode = Global.getTreeviewPNode(
          nodeMap,
          nodes,
          table.getValueByName('PID')
        )
        // data
        node['id'] = table.getValueByName('ID')
        node['pid'] = table.getValueByName('PID')
        node['name'] = table.getValueByName('UserName')
        node['userId'] = table.getValueByName('UserID')
        node['text'] = node['name']
        node['type'] = 1
        node.icon = 'yonghu'
        if (this.params.mySelfId && this.params.mySelfId === node.id) {
          table.next()
          continue
        }
        // 多选回显选中 ids为UserID
        if (this.params.ids !== undefined && this.params.isMulti) {
          var useridList = this.params.ids.split(',') || []
          if (useridList.indexOf(node.userId) !== -1) {
            this.treeDefaultCheckedKeys.push(node.id)
            this.treeDefaultExpandedKeys.push(node.id)
          }
        }
        // 单选回显选中 ids为UserID
        if (this.params.ids !== undefined && !this.params.isMulti) {
          if (this.params.ids === node.userId) {
            this.treeDefaultExpandedKeys.push(node.id)
            this.$refs.tree.setSelect(node.id)
          }
        }
        pnode.push(node)
        nodeMap.set(node.id, node)
        table.next()
      }
      // 单选回显选中 ids为ID
      if (this.params.ids !== undefined && !this.params.isMulti) {
        this.treeDefaultExpandedKeys.push(this.params.ids)
        this.$refs.tree.setSelect(this.params.ids)
      }
      this.treeData = nodes
    }
  }
}
</script>

<style scoped>
</style>
